﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Lion.WeiXin.Core.Data
{
    public class OrderDAO : IOrder
    {
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int AID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Order");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
					new SqlParameter("@AID", SqlDbType.Int,4)
			};
            parameters[0].Value = AID;

            return SqlHelper.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Models.OrderModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Order(");
            strSql.Append("Code,PrePay,Total,Payment,PayType,Creator,CreateDate,Buyer,LastUpdateDate,Buyer_Phone,Buyer_Note,Buyer_Appointment,Status,ComeFrom)");
            strSql.Append(" values (");
            strSql.Append("@Code,@PrePay,@Total,@Payment,@PayType,@Creator,@CreateDate,@Buyer,@LastUpdateDate,@Buyer_Phone,@Buyer_Note,@Buyer_Appointment,@Status,@ComeFrom)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,20),
					new SqlParameter("@PrePay", SqlDbType.Decimal,9),
					new SqlParameter("@Total", SqlDbType.Decimal,9),
					new SqlParameter("@Payment", SqlDbType.Decimal,9),
					new SqlParameter("@PayType", SqlDbType.VarChar,50),
					new SqlParameter("@Creator", SqlDbType.VarChar,50),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@Buyer", SqlDbType.VarChar,50),
					new SqlParameter("@LastUpdateDate", SqlDbType.DateTime),
					new SqlParameter("@Buyer_Phone", SqlDbType.Char,11),
					new SqlParameter("@Buyer_Note", SqlDbType.NVarChar,200),
                    new SqlParameter("@Buyer_Appointment", SqlDbType.DateTime),
					new SqlParameter("@Status", SqlDbType.Int,4),
                    new SqlParameter("@ComeFrom", SqlDbType.NVarChar,10) };
            parameters[0].Value = model.Code;
            parameters[1].Value = model.PrePay;
            parameters[2].Value = model.Total;
            parameters[3].Value = model.Payment;
            parameters[4].Value = model.PayType;
            parameters[5].Value = model.Creator;
            parameters[6].Value = model.CreateDate;
            parameters[7].Value = model.Buyer;
            parameters[8].Value = model.LastUpdateDate;
            parameters[9].Value = model.Buyer_Phone;
            parameters[10].Value = model.Buyer_Note;
            parameters[11].Value = model.Buyer_Appointment;
            parameters[11].Value = model.Status;
            parameters[12].Value = model.ComeFrom;

            object obj = SqlHelper.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Models.OrderModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Order set ");
            strSql.Append("Code=@Code,");
            strSql.Append("PrePay=@PrePay,");
            strSql.Append("Total=@Total,");
            strSql.Append("Payment=@Payment,");
            strSql.Append("PayType=@PayType,");
            strSql.Append("Creator=@Creator,");
            strSql.Append("CreateDate=@CreateDate,");
            strSql.Append("Buyer=@Buyer,");
            strSql.Append("LastUpdateDate=@LastUpdateDate,");
            strSql.Append("Buyer_Phone=@Buyer_Phone,");
            strSql.Append("Buyer_Note=@Buyer_Note,");
            strSql.Append("Buyer_Appointment=@Buyer_Appointment,");
            strSql.Append("Status=@Status");
            strSql.Append("ComeFrom=@ComeFrom");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,20),
					new SqlParameter("@PrePay", SqlDbType.Decimal,9),
					new SqlParameter("@Total", SqlDbType.Decimal,9),
					new SqlParameter("@Payment", SqlDbType.Decimal,9),
					new SqlParameter("@PayType", SqlDbType.VarChar,50),
					new SqlParameter("@Creator", SqlDbType.VarChar,50),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@Buyer", SqlDbType.VarChar,50),
					new SqlParameter("@LastUpdateDate", SqlDbType.DateTime),
					new SqlParameter("@Buyer_Phone", SqlDbType.Char,11),
					new SqlParameter("@Buyer_Note", SqlDbType.NVarChar,200),
                    new SqlParameter("@Buyer_Appointment", SqlDbType.DateTime),
					new SqlParameter("@Status", SqlDbType.Int,4),
                    new SqlParameter("@ComeFrom", SqlDbType.NVarChar,10),
					new SqlParameter("@AID", SqlDbType.Int,4)};
            parameters[0].Value = model.Code;
            parameters[1].Value = model.PrePay;
            parameters[2].Value = model.Total;
            parameters[3].Value = model.Payment;
            parameters[4].Value = model.PayType;
            parameters[5].Value = model.Creator;
            parameters[6].Value = model.CreateDate;
            parameters[7].Value = model.Buyer;
            parameters[8].Value = model.LastUpdateDate;
            parameters[9].Value = model.Buyer_Phone;
            parameters[10].Value = model.Buyer_Note;
            parameters[11].Value = model.Buyer_Appointment;
            parameters[12].Value = model.Status;
            parameters[13].Value = model.ComeFrom;
            parameters[14].Value = model.AID;

            int rows = SqlHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int AID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Order ");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
					new SqlParameter("@AID", SqlDbType.Int,4)
			};
            parameters[0].Value = AID;

            int rows = SqlHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string AIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Order ");
            strSql.Append(" where AID in (" + AIDlist + ")  ");
            int rows = SqlHelper.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.OrderModel GetModel(int AID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 AID,Code,PrePay,Total,Payment,PayType,Creator,CreateDate,Buyer,Buyer_Phone,Buyer_Note,LastUpdateDate,Buyer_Appointment,Status,ComeFrom from T_Order ");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
					new SqlParameter("@AID", SqlDbType.Int,4)
			};
            parameters[0].Value = AID;

            Models.OrderModel model = new Models.OrderModel();
            DataSet ds = SqlHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return ConvertToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return new Models.OrderModel();
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.OrderModel GetModel(string orderCode)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 AID,Code,PrePay,Total,Payment,PayType,Creator,CreateDate,Buyer,Buyer_Phone,Buyer_Note,LastUpdateDate,Buyer_Appointment,Status,ComeFrom from T_Order ");
            strSql.Append(" where Code=@Code");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,50)
			};
            parameters[0].Value = orderCode;

            Models.OrderModel model = new Models.OrderModel();
            DataSet ds = SqlHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return ConvertToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return new Models.OrderModel();
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.OrderModel ConvertToModel(DataRow row)
        {
            Models.OrderModel model = new Models.OrderModel();
            if (row != null)
            {
                if (row["AID"] != null && row["AID"].ToString() != "")
                {
                    model.AID = int.Parse(row["AID"].ToString());                    
                }
                if (row["Code"] != null)
                {
                    model.Code = row["Code"].ToString();
                    //查询是否有订单详细内容
                    model.OrderItems = this.GetOrderDetails(model.Code);
                }
                if (row["PrePay"] != null && row["PrePay"].ToString() != "")
                {
                    model.PrePay = decimal.Parse(row["PrePay"].ToString());
                }
                if (row["Total"] != null && row["Total"].ToString() != "")
                {
                    model.Total = decimal.Parse(row["Total"].ToString());
                }
                if (row["Payment"] != null && row["Payment"].ToString() != "")
                {
                    model.Payment = decimal.Parse(row["Payment"].ToString());
                }
                if (row["PayType"] != null && row["PayType"].ToString() != "")
                {
                    model.PayType = row["PayType"].ToString();
                }
                if (row["Creator"] != null)
                {
                    model.Creator = row["Creator"].ToString();
                }
                if (row["CreateDate"] != null && row["CreateDate"].ToString() != "")
                {
                    model.CreateDate = DateTime.Parse(row["CreateDate"].ToString());
                }
                if (row["Buyer"] != null)
                {
                    model.Buyer = row["Buyer"].ToString();
                    if(!string.IsNullOrEmpty(model.Buyer))
                    {
                        DBContext db = new DBContext();
                        Models.VipCardModel cardModel= db.VipCards.GetModel(model.Buyer);
                        if (cardModel != null)
                        {
                            model.BuyerName = cardModel.RealName;
                        }
                    }
                }

                if (row["Buyer_Phone"] != null)
                {
                    model.Buyer_Phone = row["Buyer_Phone"].ToString();
                }
                if (row["Buyer_Note"] != null)
                {
                    model.Buyer_Note = row["Buyer_Note"].ToString();
                }
                if (row["LastUpdateDate"] != null && row["LastUpdateDate"].ToString() != "")
                {
                    model.LastUpdateDate = DateTime.Parse(row["LastUpdateDate"].ToString());
                }
                if (row["Status"] != null && row["Status"].ToString() != "")
                {
                    model.Status = int.Parse(row["Status"].ToString());
                }
                if (row["Buyer_Appointment"] != null)
                {
                    model.Buyer_Appointment = DateTime.Parse(row["Buyer_Appointment"].ToString());
                }
                if (row["ComeFrom"] != null)
                {
                    model.ComeFrom = row["ComeFrom"].ToString();
                }
             

            }
            return model;
        }

        protected List<Models.OrderModel> ConvertToList(DataSet ds)
        {
            List<Models.OrderModel> list = new List<Models.OrderModel>();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                Models.OrderModel model = ConvertToModel(row);
                list.Add(model);
            }
            return list;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Models.OrderModel> GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select AID,Code,PrePay,Total,Payment,PayType,Creator,CreateDate,Buyer,Buyer_Phone,Buyer_Note,Buyer_Appointment,LastUpdateDate,Status,ComeFrom ");
            strSql.Append(" FROM T_Order ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            DataSet ds = SqlHelper.Query(strSql.ToString());
            return ConvertToList(ds);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public List<Models.OrderModel> GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" AID,Code,PrePay,Total,Payment,PayType,Creator,CreateDate,Buyer,Buyer_Phone,Buyer_Note,Buyer_Appointment,LastUpdateDate,Status,ComeFrom ");
            strSql.Append(" FROM T_Order ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            DataSet ds = SqlHelper.Query(strSql.ToString());
            return ConvertToList(ds);
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_Order ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = SqlHelper.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public List<Models.OrderModel> GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.AID desc");
            }
            strSql.Append(")AS Row, T.*  from T_Order T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            DataSet ds = SqlHelper.Query(strSql.ToString());
            return ConvertToList(ds);
        }

        /// <summary>
        /// 保存订单,返回订单ID
        /// </summary>
        /// <param name="model">订单实体</param>
        /// <param name="productId">产品ID</param>
        /// <param name="cardId">卡号ID</param>
        /// <returns></returns>
        public int Add(Models.OrderModel model, List<Models.Order_DetailsModel> products)
        {
            int result = 0;
            using (SqlConnection conn = SqlHelper.CreateConn())
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                SqlTransaction tx = conn.BeginTransaction();
                cmd.Transaction = tx;
                try
                {
                    //创建订单
                    StringBuilder strSql = new StringBuilder();
                    strSql.Append("insert into T_Order(");
                    strSql.Append("Code,PrePay,Total,Payment,PayType,Creator,Buyer,Buyer_Phone,Buyer_Note,Buyer_Appointment,Status,ComeFrom)");
                    strSql.Append(" values (");
                    strSql.Append("@Code,@PrePay,@Total,@Payment,@PayType,@Creator,@Buyer,@Buyer_Phone,@Buyer_Note,@Buyer_Appointment,@Status,@ComeFrom)");
                    strSql.Append(";select @@IDENTITY");
                    SqlParameter[] parameters = {
				        new SqlParameter("@Code", SqlDbType.VarChar,20),
				        new SqlParameter("@PrePay", SqlDbType.Decimal,9),
				        new SqlParameter("@Total", SqlDbType.Decimal,9),
				        new SqlParameter("@Payment", SqlDbType.Decimal,9),
				        new SqlParameter("@PayType", SqlDbType.VarChar,50),
				        new SqlParameter("@Creator", SqlDbType.VarChar,50),
				        new SqlParameter("@Buyer", SqlDbType.VarChar,50),
                        new SqlParameter("@Buyer_Phone", SqlDbType.VarChar,50),
                        new SqlParameter("@Buyer_Note", SqlDbType.VarChar,2000),
                        new SqlParameter("@Buyer_Appointment", SqlDbType.DateTime,8),
                        new SqlParameter("@Status", SqlDbType.Int,4),
                        new SqlParameter("@ComeFrom", SqlDbType.NVarChar,10),
                                                };
                    parameters[0].Value = model.Code;
                    parameters[1].Value = model.PrePay;
                    parameters[2].Value = model.Total;
                    parameters[3].Value = model.Payment;
                    parameters[4].Value = model.PayType;
                    parameters[5].Value = model.Creator;
                    parameters[6].Value = model.Buyer;
                    parameters[7].Value = model.Buyer_Phone;
                    parameters[8].Value = model.Buyer_Note;
                    parameters[9].Value = model.Buyer_Appointment;
                    parameters[10].Value = model.Status;
                    parameters[11].Value = model.ComeFrom;
                    cmd.Parameters.AddRange(parameters);
                    cmd.CommandText = strSql.ToString();
                    cmd.CommandType = CommandType.Text;
                    object obj = cmd.ExecuteScalar();
                    int orderId = int.Parse(obj.ToString());
                    cmd.Parameters.Clear();
                    //创建订单详细
                    foreach (Models.Order_DetailsModel dModel in products)
                    {

                        StringBuilder strSqlDetails = new StringBuilder();
                        strSqlDetails.Append("insert into T_Order_Details(");
                        strSqlDetails.Append("Pro_ID,Order_ID,Discount,Quantity,Price,BuyPrice,ProductName,Image)");
                        strSqlDetails.Append(" values (");
                        strSqlDetails.Append("@Pro_ID,@Order_ID,@Discount,@Quantity,@Price,@BuyPrice,@ProductName,@Image);");
                        SqlParameter[] parametersDetails = {
				        new SqlParameter("@Pro_ID", SqlDbType.Int,4),
				        new SqlParameter("@Order_ID", SqlDbType.Char,20),
				        new SqlParameter("@Discount", SqlDbType.Decimal,9),
				        new SqlParameter("@Quantity", SqlDbType.Int,4),
				        new SqlParameter("@Price", SqlDbType.Decimal,9),
                        new SqlParameter("@BuyPrice", SqlDbType.Decimal,9),
                        new SqlParameter("@ProductName", SqlDbType.VarChar,50),
                        new SqlParameter("@Image", SqlDbType.VarChar,255)
                            };
                        parametersDetails[0].Value = dModel.Pro_ID;
                        parametersDetails[1].Value = model.Code;  //关联订单ID
                        parametersDetails[2].Value = dModel.Discount;
                        parametersDetails[3].Value = dModel.Quantity;
                        parametersDetails[4].Value = dModel.Price;
                        parametersDetails[5].Value = dModel.BuyPrice;
                        parametersDetails[6].Value = dModel.ProductName;
                        parametersDetails[7].Value = dModel.Image;

                        //执行
                        cmd.Parameters.AddRange(parametersDetails);
                        cmd.CommandText = strSqlDetails.ToString();
                        cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                    }
                    //提交事物
                    tx.Commit();
                    result = orderId;
                }
                catch (System.Data.SqlClient.SqlException E)
                {
                    tx.Rollback();
                    throw new Exception(E.Message);
                }
            }
            return result;
        }

        public int PayBill(Models.OrderModel model)
        {
            //采用事物，更新订单状态和数据，
            // 从卡里扣除余额
            int result = 0;
            using (SqlConnection conn = SqlHelper.CreateConn())
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                SqlTransaction tx = conn.BeginTransaction();
                cmd.Transaction = tx;
                try
                {
                    //1 更新订单状态和实际支付款
                    StringBuilder strSql = new StringBuilder();
                    strSql.Append("update T_Order set ");
                    strSql.Append("Payment=@Payment,");
                    strSql.Append("LastUpdateDate=@LastUpdateDate,");
                    strSql.Append("Buyer_Phone=@Buyer_Phone,");
                    strSql.Append("Buyer_Note=@Buyer_Note,");
                    strSql.Append("Status=@Status");
                    strSql.Append(" where Code=@Code");
                    SqlParameter[] parameters = {
                    new SqlParameter("@Payment", SqlDbType.Decimal,9),
                    new SqlParameter("@LastUpdateDate", SqlDbType.DateTime),
					new SqlParameter("@Buyer_Phone", SqlDbType.VarChar,20),
                    new SqlParameter("@Buyer_Note", SqlDbType.VarChar,20),
					new SqlParameter("@Status", SqlDbType.Int,4),					
					new SqlParameter("@Code", SqlDbType.Char,20)};
                    parameters[0].Value = model.Payment;
                    parameters[1].Value = model.LastUpdateDate;
                    parameters[2].Value = model.Buyer_Phone;
                    parameters[3].Value = model.Buyer_Note;
                    parameters[4].Value = model.Status;
                    parameters[5].Value = model.Code;

                    cmd.Parameters.AddRange(parameters);
                    cmd.CommandText = strSql.ToString();
                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();
                    cmd.Parameters.Clear();
                    //2 扣除订单金额
                    strSql.Clear();
                    strSql.Append("update T_VipCard set ");
                    if (model.PayType == Lion.WeiXin.Core.Models.OrderPayTypeEnum.计次卡.ToString())
                    {
                        strSql.Append("Balance=Balance-@Total");
                    }
                    else {
                        strSql.Append("Balance=Balance-@Total,Total=Total+@Total");
                    }
                   
                    strSql.Append(" where Code=@Code");
                    parameters = new SqlParameter[]{
                            new SqlParameter("@Total", SqlDbType.Decimal,9),
                            new SqlParameter("@Code", SqlDbType.VarChar,50)};
                    parameters[0].Value = model.Total;
                    parameters[1].Value = model.Buyer;

                    cmd.Parameters.AddRange(parameters);
                    cmd.CommandText = strSql.ToString();
                    cmd.ExecuteNonQuery();
                    cmd.Parameters.Clear();

                    //3 更新产品销量
                    foreach (Models.Order_DetailsModel dModel in model.OrderItems)
                    {
                        StringBuilder strSqlDetails = new StringBuilder();
                        strSqlDetails.Append("update t_product set sale=sale+@Quantity where aid=@Pro_ID;");
                        SqlParameter[] parametersDetails = {
                        new SqlParameter("@Quantity", SqlDbType.Int,4),
				        new SqlParameter("@Pro_ID", SqlDbType.Int,4)
                        
                            };
                        parametersDetails[0].Value = dModel.Quantity;
                        parametersDetails[1].Value = dModel.Pro_ID;

                        //执行
                        cmd.Parameters.AddRange(parametersDetails);
                        cmd.CommandText = strSqlDetails.ToString();
                        cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                    }   

                    //提交事物
                    tx.Commit();
                    result = 1;
                }
                catch (System.Data.SqlClient.SqlException E)
                {
                    tx.Rollback();
                    Tools.Log.LogHelper.Error(E, E.Message);
                }
            }
            return result;
        }


        public List<Models.Order_DetailsModel> GetOrderDetails(string orderID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select AID,Pro_ID,ProductName,Price,BuyPrice,Order_ID,Discount,Quantity,Image from T_Order_Details ");
            strSql.Append(" where Order_ID=@OrderID");
            SqlParameter[] parameters = {
					new SqlParameter("@OrderID", SqlDbType.Char,20)
			};
            parameters[0].Value = orderID;

            DataSet ds = SqlHelper.Query(strSql.ToString(), parameters);
            List<Models.Order_DetailsModel> list = new List<Models.Order_DetailsModel>();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                Models.Order_DetailsModel model = new Models.Order_DetailsModel();
                model.AID = int.Parse(row["AID"].ToString());
                model.ProductName = row["ProductName"].ToString();
                model.Pro_ID = int.Parse(row["Pro_ID"].ToString());
                model.Price = decimal.Parse(row["Price"].ToString());
                model.BuyPrice = decimal.Parse(row["BuyPrice"].ToString());
                model.Order_ID = row["Order_ID"].ToString();
                model.Discount = decimal.Parse(row["Discount"].ToString());
                model.Quantity = int.Parse(row["Quantity"].ToString());
                model.Image = row["Image"].ToString();
                list.Add(model);
            }
            return list;
        }


        

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<Models.OrderModel> GetList(int PageSize, int PageIndex, string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "T_Order";
            parameters[1].Value = "AID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 1;
            parameters[6].Value = strWhere;
            DataSet ds = SqlHelper.RunProcedure("UP_GetRecordByPage", parameters, "ds");
            return ConvertToList(ds);
        }
    }
}
